package com.itany.mybatis;

import com.itany.mybatis.dao.HeroDao;
import com.itany.mybatis.dao.UserDao;
import com.itany.mybatis.entity.User;
import com.itany.mybatis.entity.User2;
import com.itany.mybatis.entity.UserDo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

/**
 * Description: 类的描述
 * Author: liudaolin@itany.com
 * Date: 2021/07/16 10:19
 * Version: 1.0 版本信息
 */
public class Test01 {
    /**
     * SqlSession 是核心, 又称之为 持久化管理器
     */
    private SqlSession session;

    // 初始化 mybatis
    @Before
    public void before() {
        // 1.创建 SqlSessionFactory 构建器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 2.加载 mybatis-config 文件, 创建 SqlSessionFactory
        SqlSessionFactory factory = builder.build(Test01.class
                .getClassLoader()
                .getResourceAsStream("mybatis-config.xml"));
        /*
        3.获取 SqlSession, 开启会话(默认开启事务, 不自动提交事务, 且不手动提交事务时,
        默认结束时会执行事务的回滚), 可以传入参数, 默认为 false, 不自动提交事务; true, 自动提交事务。
         */
        session = factory.openSession();
        System.out.println("start");
    }

    @After
    public void after() {
        // 关闭会话
        if(null != session){
            session.close();
        }
        System.out.println("end");
    }

    @Test
    public void f1(){
        // 执行添加操作sql
        // 直接使用 mapper 文件

        // 通过 SqlSession 调用对应的 标签的方法
        /*
        第一个参数是 mapper文件中的该标签的 完整的 标识：
        即由 mapper文件 标识 + "." + 该标签的标识 组成的 字符串
         */
        session.insert("abc.insertUser1");

        // 提交事务, 否则结束时, 默认执行回滚
        session.commit();
    }

    @Test
    public void f2() {
        // 虽然 mapper 与 dao 进行了关联, 但依然可以使用 最基本的方式, 访问
//        session.insert("com.itany.mybatis.dao.UserDao.insertUser2");

        // 使用 dao 接口这种配置的 特殊用法
        // 参数是 需要调用的方法所在接口的 class, 返回该接口的实现类
        UserDao userDao = session.getMapper(UserDao.class);
        // 调用 dao 对应的方法, 执行对应的 sql
        userDao.insertUser2();

        // 提交事务, 否则结束时, 默认执行回滚
        session.commit();
    }
    @Test
    public void f3() {
        User user = new User(null, "sssss", "111", 23);
        // 使用 dao 接口这种配置的 特殊用法
        // 参数是 需要调用的方法所在接口的 class, 返回该接口的实现类
        UserDao userDao = session.getMapper(UserDao.class);
        // 调用 dao 对应的方法, 执行对应的 sql
        userDao.insertUser3(user);

        // 提交事务, 否则结束时, 默认执行回滚
        session.commit();

        // 事务回滚
//        session.rollback();
    }

    @Test
    public void f4() {
        UserDao userDao = session.getMapper(UserDao.class);

        List<User> users = userDao.selectAll();
        users.forEach(System.out::println);

        session.commit();
    }

    @Test
    public void f5() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = new User(2, "qqqqqqq", "111111", 30);
        userDao.updateById(user);
        session.commit();
    }
    @Test
    public void f6() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = new User();
        user.setId(2);
        User byId = userDao.selectById(user);
        System.out.println(byId);
        session.commit();
    }
    @Test
    public void f61() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = new User();
        user.setId(3);
        Set<User> users = userDao.selectById2(user);
        System.out.println(users);
        session.commit();
    }
    @Test
    public void f62() {
        UserDao userDao = session.getMapper(UserDao.class);
        // 多个结果无法对应一个返回值
        User u = userDao.selectAll1();
        System.out.println(u);
        session.commit();
    }
    @Test
    public void f7() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = new User();
        user.setId(2);
        userDao.deleteById(user);
        session.commit();
    }

    @Test
    public void f8() {
        User user = new User(null, "sssss", "111", 23);
        UserDao userDao = session.getMapper(UserDao.class);
        // 添加并返回主键, 主键存在 参数中
        System.out.println("1 id:" + user.getId());
        userDao.insertUserReturnKey(user);
        System.out.println("2 id:" + user.getId());
        System.out.println("2 age:" + user.getAge());
        session.commit();
    }

    @Test
    public void f9() {
        UserDao userDao = session.getMapper(UserDao.class);
        List<HashMap<String, String>> list = userDao.selectAll3();
        list.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f10() {
        UserDao userDao = session.getMapper(UserDao.class);
        List<User> users = userDao.selectAll4();
        users.forEach(System.out::println);
        session.commit();
    }
    @Test
    public void f11() {
        UserDao userDao = session.getMapper(UserDao.class);
        List<User2> users = userDao.selectAll5();
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f12() {
        UserDao userDao = session.getMapper(UserDao.class);
        List<User2> users = userDao.selectAll6();
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f13() {
        UserDao userDao = session.getMapper(UserDao.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("nameLike", "%s%");
        List<User> users = userDao.selectByMap(map);
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f14() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = userDao.selectByUserId(12);
        System.out.println(user);
        session.commit();
    }

    @Test
    public void f15() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = userDao.selectByUserId2(12);
        System.out.println(user);
        session.commit();
    }

    @Test
    public void f16() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = userDao.selectByNameAndPwd("aaa", "111");
        System.out.println(user);
        session.commit();
    }
    @Test
    public void f17() {
        UserDao userDao = session.getMapper(UserDao.class);
        User user = userDao.selectByNameAndPwd2("111", "s");
        System.out.println(user);
        session.commit();
    }
    @Test
    public void f18() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
        u.setName("%s%");
        List<User> users = userDao.selectByNameAndAgeRange(u, 1, 100);
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f19() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
//        u.setId(12);
//        u.setName("sss");
        u.setName("");
        u.setAge(23);
        List<User> users = userDao.selectByFuzzy1(u);
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f20() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
        u.setId(12);
        u.setName("sss");
        u.setAge(20);
        userDao.updateById2(u);
        session.commit();
    }

    @Test
    public void f21() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
//        u.setId(12);
//        u.setName("sss");
//        u.setName("");
//        u.setAge(23);
        List<User> users = userDao.selectByFuzzy2(u);
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f22() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
        u.setId(12);
        u.setName("sss");
        u.setAge(20);
        userDao.updateById3(u);
//        session.commit();
    }

    @Test
    public void f23() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
        u.setId(12);
//        u.setName("sss");
//        u.setName("");
//        u.setAge(23);
        List<User> users = userDao.selectByFuzzy3(u);
        users.forEach(System.out::println);
        session.commit();
    }

    @Test
    public void f24() {
        UserDao userDao = session.getMapper(UserDao.class);
        User u = new User();
        u.setId(12);
        u.setName("sss");
        u.setAge(20);
        userDao.updateById4(u);
//        session.commit();
    }

    @Test
    public void f25() {
        UserDao userDao = session.getMapper(UserDao.class);
        String name = "%s%";
//        String name = null;

        List<User> users = userDao.selectByNameLike(name);
        users.forEach(System.out::println);
    }

    @Test
    public void f26() {
        UserDao userDao = session.getMapper(UserDao.class);
        int[] ids = {1,3,4,6};
        userDao.deleteByIds1(ids);
    }
    @Test
    public void f27() {
        UserDao userDao = session.getMapper(UserDao.class);
        List<Integer> list = Arrays.asList(1,3,4,6);
        userDao.deleteByIds2(list);
    }
    @Test
    public void f28() {
        UserDao userDao = session.getMapper(UserDao.class);

        UserDo userDo = new UserDo();
        Integer[] ids = {1,3,4,6};
        userDo.setIds(ids);

        userDao.deleteByIds3(userDo);
    }
    @Test
    public void f29() {
        UserDao userDao = session.getMapper(UserDao.class);

        Map<String, List<Integer>> map = new HashMap<>();
        map.put("abc", Arrays.asList(1,3,4,6,8));

        userDao.deleteByIds4(map);
    }
    @Test
    public void f30() {
        UserDao userDao = session.getMapper(UserDao.class);
//        int[] ids = {1,3,4,6};
        int[] ids = {};
        List<User> users = userDao.selectByIds1(null);
        users.forEach(System.out::println);
    }
    @Test
    public void f31() {
        UserDao userDao = session.getMapper(UserDao.class);
//        int[] ids = {1,3,4,6};
        int[] ids = {};
        List<User> users = userDao.selectByIds2(ids);
        users.forEach(System.out::println);
    }
    @Test
    public void f33() {
        UserDao userDao = session.getMapper(UserDao.class);
        int type = 1;
        List<User> users = userDao.selectByType(type);
        users.forEach(System.out::println);
        session.commit();
    }

    // TODO: homework 2021-07-19
    @Test
    public void homework() {
        UserDao userDao = session.getMapper(UserDao.class);
        // 示例参数
        Map<String, Object> param = new HashMap<>();
        param.put("tableName", "t_user");
        param.put("columnArr", new String[]{ "name","pwd","age" });
        param.put("valueArr", new Object[]{ "tom11","1", 22 });

        userDao.insertObject(param);

        session.commit();
    }

}
